<template>
  <div>
    <el-dialog
      width="1000px"
      :title="$t('角色管理')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form :model="queryParams" ref="queryForm" size="small" inline>
        <el-form-item prop="roleName">
          <el-input
            v-model="queryParams.roleName"
            placeholder="角色名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="getList"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['distribution:roleDistr:insertD']"
          >
            新增
          </el-button>
        </el-col>
      </el-row>

      <el-table :height="calTableHeight() - 180" v-loading="loading" ref="TableRef" :data="tableData">
        <el-table-column label="角色名称" align="center" prop="roleName" :show-overflow-tooltip="true" />
        <el-table-column label="显示顺序" align="center" prop="roleSort" />
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['distribution:roleDistr:editD']"
            >
              {{ $t('修改') }}
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['distribution:roleDistr:deleteD']"
            >
              {{ $t('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <div slot="footer">
        <el-button @click="cancel">
          {{ $t('关闭') }}
        </el-button>
      </div>
    </el-dialog>
    <AddAndEdit
      :show.sync="addAndEdit.show"
      :type="addAndEdit.type"
      :formData="addAndEdit.formData"
      @getList="getList"
    />
  </div>
</template>

<script>
import { listRole } from '@/api/platform/role'
import { deleteD } from '@/api/platform/supplierManage'

import AddAndEdit from './addAndEdit.vue'

export default {
  components: { AddAndEdit },
  props: {
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      addAndEdit: {
        show: false,
        type: '',
        formData: {},
      },

      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 角色表格数据
      tableData: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined,
      },
    }
  },
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true
      listRole({
        ...this.queryParams,
        distrId: this.formData.distrId,
      }).then((response) => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    cancel() {
      this.$emit('update:show', false)
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.addAndEdit.type = 'add'
      this.addAndEdit.show = true
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.addAndEdit.type = 'edit'
      this.addAndEdit.formData = { ...row }
      this.addAndEdit.show = true
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.roleIdDistr
      this.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项？').then(() => {
        const data = {
          id: row.roleIdDistr,
          distrId: JSON.parse(localStorage.getItem('distr') || '{}').distrId,
        }
        deleteD(data).then((res) => {
          this.$message({ type: 'success', message: res.msg })
          this.getList()
        })
      })
    },
  },
}
</script>
